package com.lepu.app.fun.my;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.os.Bundle;
import android.text.InputFilter;
import android.widget.EditText;

import com.app.application.MyApplication;
import com.app.utils.LoginUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.EmojiFilter;
import com.core.lib.utils.main.UIHelper;
import com.lepu.app.usercenter.bean.LoginInfo;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewRightButtonListener;
import com.lepu.pasm.R;

public class MyUserModifyNameActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener, OnTopbarNewRightButtonListener, AsyncRequest{

	private static final String REQUEST_DATA = "request_data";
	private String mName = null;
	private  EditText editText;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_user_modifyname_activity);

		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}
	
	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	@Override
	public void onTopbarRightButtonSelected() {
		request();
	}
	
	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("设置昵称");
		topbar.setRightText("保存");
		topbar.setonTopbarNewLeftLayoutListener(this);
		topbar.setOnTopbarNewRightButtonListener(this);
		
		editText = (EditText) findViewById(R.id.editText);
		editText.setFilters(new InputFilter[] { new EmojiFilter(),new InputFilter.LengthFilter(14) });
	}

	// 请求数据
	public void request() {
        mName = editText.getText().toString();
		if(mName == null || mName.equals("")){
			UIHelper.showToast(this, "请输入昵称");
			return;
		}
		
		String url = Setting.getModifyNameUrl();

		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();
		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);

			params.put("UserID", LoginUtil.getUserId());
			params.put("LoginToken", LoginUtil.getLoginToken());
			params.put("NickName", mName);
		} catch (Exception e) {
		}
		map.put("ht", params);
		ApiClient.http_post_main(url, map, null, this,REQUEST_DATA, true, false);
		showProgressDialog();
	}
	
	@Override
	public void RequestComplete(Object arg0, Object arg1) {
		hideProgressDialog();
		LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
		loginInfo.NickName = mName;
		
		LoginInfo.saveLoginInfo(MyUserModifyNameActivity.this, loginInfo);
		
		UIHelper.showToast(MyUserModifyNameActivity.this, "昵称修改成功");
		finish(true);
	}

	@Override
	public void RequestError(Object arg0, int arg1, String arg2) {
		hideProgressDialog();
	}

}
